package com.wz.dhs.controller.system;

import com.wz.dhs.exception.BusinessException;
import com.wz.dhs.model.UserContextHolder;
import com.wz.dhs.model.common.PageInfo;
import com.wz.dhs.model.common.Result;
import com.wz.dhs.po.user.SysConfig;
import com.wz.dhs.service.base.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 参数配置 信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/config")
public class SysConfigController {
    @Autowired
    private ISysConfigService configService;


    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public Result<PageInfo<SysConfig>> list(SysConfig config) {

        PageInfo<SysConfig> list = configService.pageConfigList(config);
        return Result.data(list);
    }


    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    public Result<SysConfig> getInfo(@PathVariable Long configId) {

        return Result.data(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public Result<String> getConfigKey(@PathVariable String configKey) {
        return Result.data(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @PostMapping
    public Result<String> add(@Validated @RequestBody SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            throw new BusinessException("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(UserContextHolder.getUsername());
        configService.insertConfig(config);
        return Result.success("修改成功");
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @PutMapping
    public Result<String> edit(@Validated @RequestBody SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            throw new BusinessException("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(UserContextHolder.getUsername());
        configService.updateConfig(config);
        return Result.success("修改成功");
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @DeleteMapping("/{configIds}")
    public Result<String> remove(@PathVariable Long[] configIds) {
        configService.deleteConfigByIds(configIds);
        return Result.success("删除成功");
    }

    /**
     * 刷新参数缓存
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @DeleteMapping("/refreshCache")
    public Result<String> refreshCache() {
        configService.resetConfigCache();
        return Result.success("刷新成功");
    }
}
